﻿// -----------------------------------------------------------------------
// <copyright file="Util.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TestApp
{
    using System.Diagnostics;
    using CSparse;
    using System.IO;
    using System.IO.Compression;
    using TestApp.Tar;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class Helper
    {
        private static Stopwatch stopwatch = new Stopwatch();

        public static long Toc()
        {
            stopwatch.Stop();
            return stopwatch.ElapsedMilliseconds;
        }

        public static void Tic()
        {
            stopwatch.Restart();
        }

        public static string GetOrderString(ColumnOrdering order)
        {
            switch (order)
            {
                case ColumnOrdering.MinimumDegreeAtPlusA:
                    return "amd(A+A')";
                case ColumnOrdering.MinimumDegreeStS:
                    return "amd(S'*S)";
                case ColumnOrdering.MinimumDegreeAtA:
                    return "amd(A'*A)";
            }

            return "natural";
        }

        public static void UnpackFile(string gzFile, string mtxFile)
        {
            if (gzFile.EndsWith(".tar.gz"))
            {
                UnpackTarArchive(gzFile, mtxFile);
            }
            else if (gzFile.EndsWith(".gz"))
            {
                UnpackGzipFile(gzFile, mtxFile);
            }
            else
            {
            }
        }

        private static void UnpackTarArchive(string gzFile, string mtxFile)
        {
            using (var instream = new FileStream(gzFile, FileMode.Open, FileAccess.Read))
            {
                using (var gzip = new GZipStream(instream, CompressionMode.Decompress))
                {
                    const int size = 4096;
                    byte[] buffer = new byte[size];

                    TarReader tar = new TarReader(gzip);
                    while (tar.MoveNext(true))
                    {
                        if (tar.FileInfo.FileName.EndsWith(mtxFile))
                        {
                            using (var outstream = new FileStream(mtxFile, FileMode.OpenOrCreate, FileAccess.Write))
                            {
                                tar.Read(outstream);
                                break;
                            }
                        }
                    }
                }
            }
        }

        private static void UnpackGzipFile(string gzFile, string mtxFile)
        {
            using (var instream = new FileStream(gzFile, FileMode.Open, FileAccess.Read))
            {
                using (var gzip = new GZipStream(instream, CompressionMode.Decompress))
                {
                    const int size = 4096;
                    byte[] buffer = new byte[size];

                    using (var outstream = new FileStream(mtxFile, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        int count = 0;
                        do
                        {
                            count = gzip.Read(buffer, 0, size);
                            if (count > 0)
                            {
                                outstream.Write(buffer, 0, count);
                            }
                        }
                        while (count > 0);
                    }
                }
            }
        }
    }
}
